<?php
/**
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2017/1/5
 * Time: 16:49
 */

namespace Home\Controller;


use Common\Model\MyCenterModel;
use Common\Model\UserAddessModel;
use Think\Controller;

class MyCenterController extends Controller
{
    //首页方法
    public function index(){

            //这里的数据需要从上个页面用隐藏域提交过来
        if(IS_POST){
            //如果能走这里就证明订单已成功付款，该写入订单的数据啦
            $myCenterModel =  new MyCenterModel();
            $res  = $myCenterModel->addOrder();
            if($res['status']=='success'){
                $this->success($res['message'],U('Home/MyCenter/index'));die;
            }else{
                $this->error($myCenterModel->getError());die;
            }


        }


        $uid   = session('home.uid');
        //关联订单表和订单详情表，再跟商品表关联
        //$Model->join('think_work ON think_artist.id = think_work.artist_id')->join('think_card ON think_artist.card_id = think_card.id')->select();
        $oldOredrData    = m('order')->alias('o')->join('shop_order_list ol on o.oid=ol.oid')->join('shop_goods g on  ol.gid=g.gid')->where("o.uid=$uid")->field('o.oid,o.number,o.time,g.pic,g.gname,g.shopprice,g.gnumbers,o.total,o.status,ol.quantity')->select();
        //获得订单数据  需要的数据 订单号，下单时间，商品图片，订单状态，收货人，下单金额。
        $name = (new UserAddessModel())->where("uid={$uid} and status=1")->getField('name');
        $this->assign('name',$name);
        $temp = [];
        foreach ($oldOredrData  as $k=>$v ){
            $temp[$v['number']][]  = $v;
        }
        $this->assign('temp',$temp);


        //分配头部信息
        $conf = [
            'js'=>['list'],
            'css'=>['index','account']
        ];
        $this->assign('conf',$conf);
        //加载模板
        $this->display();
    }

    //删除订单
    public  function del(){
        //获得当前的删除的订单id
        $oid  = I('get.oid');
        $mycenterData  =  new MyCenterModel();
        if($mycenterData->where("oid={$oid}")->delete()){
            $this->sucess('删除成功');
        }else{
            $this->error('删除失败');
        }
    }



    //地址管理
    public function addess(){

        $conf= [
            'js'=>['account','area'],
            'css'=>['account']
        ];
        $orderData  = $_SESSION['order'];
        //查询数据库获得已存在的添加地址
        $userAddessModel = new UserAddessModel();
        $oldUserData  = $userAddessModel->select();
        //获得用户uid
        $uid = session('home.uid');
        //获取用户的默认地址
        $oldMoRenData = $userAddessModel->where("uid={$uid} AND status=1")->find();
        //分配数据
        $this->assign('oldMoRenData',$oldMoRenData);
        $this->assign('oldUserData',$oldUserData);
        $this->assign('conf',$conf);
        //加载模板
        $this->display();
    }


}